package net.xiduth.game.player.content.grandexchange;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;

import net.xiduth.utils.SerializableFilesManager;

/**
 * @author Tyluur <itstyluur@gmail.com>
 * @since 2012-08-17
 */
public class WorldOffers {

	/** The array of the buttons used to send the buying interfaces */
	public static final int[] BUY_BUTTON_IDS = { 31, 82, 101, 47, 63, 120 };

	/** The array of the buttons used to initaliaze the selling process */
	public static final int[] SELL_BUTTON_IDS = { 83, 32, 48, 102, 121, 64 };

	/** The array of the items used to collect the rewards */
	public static final int[] COLLECT_BUTTON_IDS = {19, 70, 89, 35, 51, 108 };

	/** The instance of the file that contains the world offers which will be stored. */
	private static File file = new File("data/items/grand_exchange/world_offers.ser");

	/** The array of the world grand exchange offers */
	private static CopyOnWriteArrayList<GrandExchangeOffer> offers;

	/** The instance of the logger in this class */
	private static final Logger logger = Logger.getLogger(WorldOffers.class.getCanonicalName());

	/**
	 * Loads the grand exchange offers from the file location.
	 */
	@SuppressWarnings("unchecked")
	public static void loadOffers() {
		try {
			if (file.exists()) {
				offers = (CopyOnWriteArrayList<GrandExchangeOffer>) SerializableFilesManager.loadSerializedFile(file);
				logger.info("Successfully loaded " + offers.size() + " grand exchange offers.");
			} else {
				offers = new CopyOnWriteArrayList<GrandExchangeOffer>();
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	/**
	 * Saves the grand exchange offers into a serializable file.
	 */
	public static void saveOffers() {
		try {
			SerializableFilesManager.storeSerializableClass(offers, file);
			logger.info("Saved " + offers.size() + " grand exchange offers.");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Gets the array of the grand exchange offers.
	 * @return The grand exchange offers
	 */
	public static CopyOnWriteArrayList<GrandExchangeOffer> getOffers() {
		return offers;
	}

}
